/*
 * Copyright (C), 2002-2017, 苏宁易购电子商务有限公司
 * FileName: AdminActivityDaoImpl.java
 * Author:   15060460
 * Date:     2017年4月6日 下午4:24:56
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.member.dao.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.member.dao.AdminActivityDao;
import com.suning.sawp.member.dto.AdminActivity;
import com.suning.sawp.po.store.StoreInfo;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 15060460
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Repository
public class AdminActivityDaoImpl implements AdminActivityDao {

	@Autowired
	protected DalClient dalClient;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.suning.sawp.member.dao.AdminActivityDao#queryActivitysHq(java.lang
	 * .String, java.lang.String)
	 */
	@Override
	public List<AdminActivity> queryActivitys(String areaCode, String role, Long start, Long limit) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("code", areaCode);
		if (StringUtils.isNotEmpty(role)) {
			paramMap.put("role", role);
		}
		if (start != null) {
			paramMap.put("start", start);
			paramMap.put("limit", limit);
		}
		return dalClient.queryForList("adminActivity.queryActivitys", paramMap, AdminActivity.class);
	}

	@Override
	public Long queryActivitysCount(String areaCode, String role) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("code", areaCode);
		if (StringUtils.isNotEmpty(role)) {
			paramMap.put("role", role);
		}
		return dalClient.queryForObject("adminActivity.queryActivitysCount", paramMap, Long.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.suning.sawp.member.dao.AdminActivityDao#queryActivitysDz(java.lang
	 * .String)
	 */
	@Override
	public List<AdminActivity> queryActivitysDz(String storeCode) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("storeCode", storeCode);
		return dalClient.queryForList("adminActivity.queryActivitysDz", paramMap, AdminActivity.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.suning.sawp.member.dao.AdminActivityDao#queryActivitysDd(java.lang
	 * .String, java.lang.String)
	 */
	@Override
	public List<AdminActivity> queryActivitysDd(String storeCode, String staffId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("storeCode", storeCode);
		paramMap.put("staffId", staffId);
		return dalClient.queryForList("adminActivity.queryActivitysDd", paramMap, AdminActivity.class);
	}

	@Override
	public int deleteActivityStore(String activityId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("activityId", activityId);
		return dalClient.execute("adminActivity.deleteActivityStore", paramMap);
	}

	@Override
	public Long countStoreCustomer(Long activityId, String storeCode) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("activityId", activityId);
		paramMap.put("storeCode", storeCode);
		return dalClient.queryForObject("adminActivity.countStoreCustomer", paramMap, Long.class);
	}

	@Override
	public void deleteActStoreAssociatedData(Long activityId, String storeCode) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		params.put("storeCode", storeCode);
		dalClient.execute("adminActivity.deleteActStoreByActIdAndStoreCode", params);
		dalClient.execute("adminActivity.deleteStoreCateByActIdAndStoreCode", params);
		dalClient.execute("adminActivity.deleteExpandByActIdAndStoreCode", params);
		dalClient.execute("adminActivity.deleteExpandEmployeeByActIdAndStoreCode", params);
	}

	public long countCustomerDetailByEmployeeId(Long activityId, String employeeId, String source) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		params.put("employeeId", employeeId);
		if (StringUtils.isNotEmpty(source)) {
			params.put("source", source);
		}
		return dalClient.queryForObject("adminActivity.countCustomerDetailByEmployeeId", params, Long.class).longValue();
	}

	public <T> List<T> queryCustomerDetailByEmployeeId(Long activityId, String employeeId, String source, int start, int end, Class<T> clazz) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		params.put("employeeId", employeeId);
		if (StringUtils.isNotEmpty(source)) {
			params.put("source", source);
		}
		params.put("start", start);
		params.put("end", end);
		return dalClient.queryForList("adminActivity.queryCustomerDetailByEmployeeId", params, clazz);
	}

	public <T> List<T> queryCustomerDetailByDZDD(Long activityId, String storeCode, String cateCodes, String source, int start, int end, Class<T> clazz) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		params.put("storeCode", storeCode);
		if (StringUtils.isNotEmpty(cateCodes)) {
			if (cateCodes.indexOf(",") > -1) {
				List<String> list = Arrays.asList(cateCodes.split(","));
				params.put("cateCodes", list);
			} else {
				params.put("cateCode", cateCodes);
			}
		}
		if (StringUtils.isNotEmpty(source)) {
			params.put("source", source);
		}
		params.put("start", start);
		params.put("end", end);
		return dalClient.queryForList("adminActivity.queryCustomerDetailByDZDD", params, clazz);
	}

	public long countCustomerDetailByDZDD(Long activityId, String storeCode, String cateCodes, String source) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("activityId", activityId);
		params.put("storeCode", storeCode);
		if (StringUtils.isNotEmpty(cateCodes)) {
			if (cateCodes.indexOf(",") > -1) {
				List<String> list = Arrays.asList(cateCodes.split(","));
				params.put("cateCodes", list);
			} else {
				params.put("cateCode", cateCodes);
			}
		}
		if (StringUtils.isNotEmpty(source)) {
			params.put("source", source);
		}
		return dalClient.queryForObject("adminActivity.countCustomerDetailByDZDD", params, Long.class).longValue();
	}

	public String queryLabelNamesByCustDetailId(Long detailId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("detailId", detailId);
		return dalClient.queryForObject("adminActivity.queryLabelNamesByCustDetailId", params, String.class);
	}

	public List<Map<String, Object>> querySaleInfo(String mobile, String custNo, Long activityId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("mobile", mobile);
		params.put("activityId", activityId);
		if (StringUtils.isEmpty(custNo)) {
			params.put("custNo", custNo);
		}
		return dalClient.queryForList("adminActivity.querySaleInfo", params);
	}

	public String queryPlaceByExpandId(String expandId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("expandId", expandId);
		return dalClient.queryForObject("adminActivity.queryPlaceByExpandId", params, String.class);
	}

	public List<StoreInfo> queryStoresBySaleBranchCode(String saleBranchCode, String storeTypeCode) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("saleBranchCode", saleBranchCode);

		if (StringUtils.isNotEmpty(storeTypeCode)) {
			if (storeTypeCode.indexOf(",") > -1) {
				List<String> list = Arrays.asList(storeTypeCode.split(","));
				params.put("storeTypeCodes", list);
			} else {
				params.put("storeTypeCode", storeTypeCode);
			}
		}

		return dalClient.queryForList("adminActivity.queryStoresBySaleBranchCode", params, StoreInfo.class);
	}

	public List<StoreInfo> queryStoresByRegionCode(String regionCode, String storeTypeCode) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("regionCode", regionCode);
		if (StringUtils.isNotEmpty(storeTypeCode)) {
			if (storeTypeCode.indexOf(",") > -1) {
				List<String> list = Arrays.asList(storeTypeCode.split(","));
				params.put("storeTypeCodes", list);
			} else {
				params.put("storeTypeCode", storeTypeCode);
			}
		}
		return dalClient.queryForList("adminActivity.queryStoresByRegionCode", params, StoreInfo.class);
	}

}
